﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CardLogistics.Model.Sys;
using System.Data.OracleClient;
using System.Data;

namespace CardLogistics.DAL.Sys
{
    /// <summary>
    /// 单据规则_数据访问类
    /// </summary>
    public class Sys_BillRuleData
    {
        #region 新增
        /// <summary>
        /// 新增信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Add(Sys_BillRule model)
        {
            try
            {
                OracleParameter[] parameters = {
					new OracleParameter("varOut", OracleType.VarChar,100),
					new OracleParameter("vBILLNAME", OracleType.NVarChar),
					new OracleParameter("vNUMONE", OracleType.NVarChar),
					new OracleParameter("vNUMTWO", OracleType.NVarChar),
					new OracleParameter("vNUMTHREE", OracleType.NVarChar),
					new OracleParameter("vNUMFOUR", OracleType.NVarChar),
					new OracleParameter("vSORT", OracleType.Number,4)};
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = model.BILLNAME;
                parameters[2].Value = model.NUMONE;
                parameters[3].Value = model.NUMTWO;
                parameters[4].Value = model.NUMTHREE;
                parameters[5].Value = model.NUMFOUR;
                parameters[6].Value = model.SORT;
                string StrOutPar = DbHelperSQL.RunProcedure("P_Sys_BillRule_Add", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>修改成功返回true、否则返回false</retu     rns>
        public string Update(Sys_BillRule model)
        {
            try
            {
                OracleParameter[] parameters = {
					new OracleParameter("vID", OracleType.VarChar,100),
					new OracleParameter("vBILLNAME", OracleType.NVarChar),
					new OracleParameter("vNUMONE", OracleType.NVarChar),
					new OracleParameter("vNUMTWO", OracleType.NVarChar),
					new OracleParameter("vNUMTHREE", OracleType.NVarChar),
					new OracleParameter("vNUMFOUR", OracleType.NVarChar),
					new OracleParameter("vSORT", OracleType.Number,4),
                    new OracleParameter("varOut", OracleType.VarChar,100)};
                parameters[0].Value = model.ID;
                parameters[1].Value = model.BILLNAME;
                parameters[2].Value = model.NUMONE;
                parameters[3].Value = model.NUMTWO;
                parameters[4].Value = model.NUMTHREE;
                parameters[5].Value = model.NUMFOUR;
                parameters[6].Value = model.SORT;
                parameters[7].Direction = ParameterDirection.Output;
                return DbHelperSQL.RunProcedure("P_Sys_BillRule_Update", parameters, parameters[7]).ToString();
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id">删除信息的ID</param>
        /// <returns>删除成功返回True。否则返回false</returns>
        public string Delete(string id)
        {
            try
            {
                OracleParameter[] parameters = {
                        new OracleParameter("VarOut",  OracleType.VarChar,50),
					    new OracleParameter("vID",  OracleType.VarChar)
                };
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = id;
                string StrOutPar = DbHelperSQL.RunProcedure("P_Sys_BillRule_Delete", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 查询
        /// <summary>
        /// 根据条件查询相应的表的字段
        /// </summary>
        /// <param name="StrWhere">查询条件</param>
        /// <param name="StrFields">要查询的字段</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetList(string StrWhere, string StrFields)
        {
            return CommonData.GetListOrderBy(StrWhere, "Sys_BillRule", StrFields, "id", 0);
        }

        /// <summary>
        /// 根据编号查询基本信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetInfoById(string id)
        {
            return GetList(" ID = '" + id + "' ", "*");
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Sys_BillRule GetModel(string userId)
        {
            DataRow row = GetInfoById(userId).Rows[0];
            Sys_BillRule model = new Sys_BillRule();
            if (row != null)
            {
                if (row["ID"] != null)
                {
                    model.ID = row["ID"].ToString();
                }
                if (row["BILLNAME"] != null)
                {
                    model.BILLNAME = row["BILLNAME"].ToString();
                }
                if (row["NUMONE"] != null)
                {
                    model.NUMONE = row["NUMONE"].ToString();
                }
                if (row["NUMTWO"] != null)
                {
                    model.NUMTWO = row["NUMTWO"].ToString();
                }
                if (row["NUMTHREE"] != null)
                {
                    model.NUMTHREE = row["NUMTHREE"].ToString();
                }
                if (row["NUMFOUR"] != null)
                {
                    model.NUMFOUR = row["NUMFOUR"].ToString();
                }
                if (row["SORT"] != null && row["SORT"].ToString() != "")
                {
                    model.SORT = decimal.Parse(row["SORT"].ToString());
                }
                if (row["LASTNUM"] != null && row["LASTNUM"].ToString() != "")
                {
                    model.LASTNUM = row["LASTNUM"].ToString();
                }
            }
            return model;
        }
        #endregion

    }
}
